package com.cndym.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: cndym
 * Date: 11-10-23
 * Time: 下午4:55
 */
public class BuildLotteryNumber {
    private static Map<Integer, String> ssqRedNumber = new HashMap<Integer, String>();
    private static Map<Integer, String> ssqBlueNumber = new HashMap<Integer, String>();
    private static Map<Integer, String> dltQNumber = new HashMap<Integer, String>();
    private static Map<Integer, String> dltHNumber = new HashMap<Integer, String>();

    static {
        forInstance_ssq();
    }

    public static void forInstance_ssq() {
        ssqRedNumber.put(0, Utils.fullByZero(1, 2));
        ssqBlueNumber.put(0, Utils.fullByZero(1, 2));
        dltQNumber.put(0, Utils.fullByZero(1, 2));
        dltHNumber.put(0, Utils.fullByZero(1, 2));
        for (int i = 1; i <= 33; i++) {
            ssqRedNumber.put(i, Utils.fullByZero(i, 2));
            if (i <= 16) {
                ssqBlueNumber.put(i, Utils.fullByZero(i, 2));
            }
        }
        for (int i = 1; i <= 35; i++) {
            dltQNumber.put(i, Utils.fullByZero(i, 2));
            if (i <= 12) {
                dltHNumber.put(i, Utils.fullByZero(i, 2));
            }
        }


    }

    public static String ssq() {
        StringBuilder buffer = new StringBuilder();
        Map<String, String> temp = new HashMap<String, String>();
        int i = 0;
        while (i < 6) {
            String ball = ssqRedNumber.get(Utils.intRandom(32));
            if (null == ball || temp.containsKey(ball)) {
                continue;
            }
            temp.put(ball, ball);
            buffer.append(",").append(ball);
            i++;
        }
        StringBuffer end = new StringBuffer();
        sort(buffer.substring(1), end);
        end.append("#");
        end.append(ssqBlueNumber.get(Utils.intRandom(15)));
        return end.substring(1);
    }

    public static String dlt() {
        StringBuilder buffer = new StringBuilder();
        Map<String, String> temp = new HashMap<String, String>();
        Map<String, String> tempH = new HashMap<String, String>();
        int i = 0;
        while (i < 5) {
            String ball = dltQNumber.get(Utils.intRandom(35));
            if (null == ball || temp.containsKey(ball)) {
                continue;
            }
            temp.put(ball, ball);
            buffer.append(",").append(ball);
            i++;
        }
        StringBuffer end = new StringBuffer();
        sort(buffer.substring(1), end);
        end.append("#");
        StringBuilder bufferH = new StringBuilder();
        int j = 0;
        while (j < 2) {
            String ball = dltHNumber.get(Utils.intRandom(12));
            if (null == ball || tempH.containsKey(ball)) {
                continue;
            }
            tempH.put(ball, ball);
            bufferH.append(",").append(ball);
            j++;
        }
        end.append(bufferH.substring(1));
        return end.substring(1);
    }

    public static void sort(String number, StringBuffer buffer) {
        String temp = "," + number;
        String[] arr = number.split(",");
        int min = 0;
        String sub = null;
        for (String s : arr) {
            int c = Utils.formatInt(s, 0);
            if (min == 0) {
                min = c;
                sub = s;
            }
            if (c < min) {
                min = c;
                sub = s;
            }
        }
        temp = temp.replace("," + sub, "");
        buffer.append(",").append(sub);
        if (!temp.equals("")) {
            sort(temp.substring(1), buffer);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(dlt());
        }
    }
}
